package com.zp.flyweight;/**
 * @description
 * @author zp
 * @created 2021/9/3 21:35
 */

import com.zp.entity.Blue;
import com.zp.entity.Color;
import com.zp.entity.Green;
import com.zp.entity.Red;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zp
 * @description
 * @created 2021/9/3 21:35
 */
public class ColorFactory {
    private static Map<String, Color> colorMap = new HashMap<String, Color>();

    public Color getColor(String colorName){
        Color color = colorMap.get(colorName);
        if(color == null){
            if("red".equals(colorName)){
                colorMap.put(colorName,new Red());
            }else if("blue".equals(colorName)){
                colorMap.put(colorName,new Blue());
            }else if("green".equals(colorName)){
                colorMap.put(colorName,new Green());
            }
        }
        return colorMap.get(colorName);
    }
}
